package com.service.Impl;

import com.entity.User;
import com.exception.AccountException;
import com.exception.GlobalException;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import com.entity.Account;
import com.mapper.AccountMapper;
import com.mapper.AccountXpInfoMapper;
import com.service.AccountService;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AccountDaoImpl
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/04/16 16:21
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountXpInfoMapper accountXpInfoMapper;

    @Override
    public int getNewAccountCount() {
        return accountMapper.getNewAccountCount();
    }

    @Override
    public int getAccountCount() {
        return accountMapper.getAccountCount();
    }

    @Override
    public void updateAccount(Integer accountId, Byte type) {
        try {
            accountMapper.updateAccount(accountId, type);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(1001, "更新账号信息失败");
        }
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public Account selectAccountByUsername(String username, String pwd) {
        Account account = accountMapper.selectAccountByUsername(username, pwd);
        if (account == null)
            throw new AccountException(1000, "用户名或密码错误");
        return account;
    }

    @Override
    public Account selectByUsername(String username) {
        return accountMapper.selectByUsername(username);
    }

    @Override
    @Transactional //开启事务,确保账号和经验积分同时操作
    public void insertAccount(Account account) {
        //1. 检查用户名是否存在
        Account existing = selectByUsername(account.getUsername());
        if (existing != null)
            throw new AccountException(1000, "该用户名已经被注册");

        //2. 对密码进行加密操作 (MD5)
        String encryptedPwd = DigestUtils.md5DigestAsHex(account.getPasswordHash().getBytes());
        account.setPasswordHash(encryptedPwd);

        //3. 设置默认注册信息
        account.setRegisterTime(LocalDateTime.now());
        account.setAccountType(1);//1-普通用户
        account.setIsdisabled(false); //0-未禁用

        //4. 插入账号信息
        try {
            accountMapper.insertAccount(account);
        } catch (Exception e) {
            throw new AccountException(1001, "插入账号信息失败");
        }

        //5.初始化账号的经验积分信息
        try {
            accountXpInfoMapper.insertAccountXpInfo(account.getAccountId());
        } catch (Exception e) {
            throw new AccountException(1002, "初始化账号经验积分失败");
        }

        //6.初始化用户信息
        try {
            // 获取默认头像路径
            String projectRoot = System.getProperty("user.dir");
            String driveLetter = projectRoot.substring(0, 3);
            String defaultImagePath = driveLetter + "Fishing_forums\\userdefaultavatar\\avatar.jpg";

            User user = new User();
            user.setAccountId(account.getAccountId());
            user.setAvatar(defaultImagePath);

            userMapper.insertUserInfo(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccountException(1003, "初始化用户信息失败");
        }

    }

    @Override
    public void updatePassword(String username, String newPwd) {
        String encryptedPwd = DigestUtils.md5DigestAsHex(newPwd.getBytes()); //进行密码加密
//        accountMapper.updatePassword(username, encryptedPwd);
        return;
    }

    @Override
    @Transactional
    public void updateAccountXpById(Integer accountId, Integer experience, Integer integral,Integer taskId) {
        try {
            accountXpInfoMapper.updateAccountXpById(accountId, experience, integral); //更新经验积分
        } catch (Exception e) {
            throw new AccountException(1003, "更新经验积分失败");
        }
    }
}
